package org.papervision3d.materials
{
	import flash.geom.ColorTransform;
	import flash.display.BitmapData;
	import flash.geom.Matrix;
	
	public class TintMaterial extends BitmapMaterial
	{
		
		public function set color( val : uint ) : void {
			if( _color == val)return;
			
			_color = val;
			
			
			var a : Number = ((_color & 0xff000000 ) >>> 24) / 256;
			var r : Number = ((_color & 0xff0000 ) >> 16) / 256;
			var v : Number = ((_color & 0xff00 ) >> 8) / 256;
			var b : Number = (_color & 0xff) /256;
			
			bitmap.fillRect( bitmap.rect, 0 );
			bitmap.draw( baseMat.bitmap, new Matrix(), new ColorTransform( r,v,b,a ) );
		}
		
		public function TintMaterial( baseMaterial : BitmapMaterial )
		{
			super( baseMaterial.bitmap, baseMaterial);
			baseMat = baseMaterial;
		}
		
		override  protected function createBitmap(asset:*):BitmapData
        {
        	var base : BitmapData = asset as BitmapData;
        	var bd : BitmapData = new BitmapData( base.width, base.height, true, 0 );
        	bd.draw( base );
            return bd;
        }
		
		
		private var _color : uint;
		private var baseMat : BitmapMaterial;
	}
}